<?php
namespace  Home\Controller;
use Think\Controller;
use Com\DingdingAuth;

class DatabaseController  extends  HomeController{
	
	public function index(){
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		// 搜索
		
		$keyword = I('keyword', '', 'string');
		
		$condition = array('like','%' . $keyword . '%');
		
		$map['name|account'] = array($condition,$condition,'_multi' => true );
	
		$data_list = D('Database')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
		
		$page = new \Common\Util\Page(D('Database')->where($map)->count(), C('ADMIN_PAGE_ROWS'));
		
		
		
		foreach ($data_list as $key => &$val) {
			$customerid=$val['customer'];
			$val['kh_id']=$customerid;
			$val['customer']=get_customer_info($val['customer'],"customer_name");
			$val['c_user']=get_user_info($val['c_user'],"username");
			$val['server']=get_fields("Server",$val['server'],"server_name");
			 
			 
			//$val['customer']="<a href='".U('customer/xq',array('id'=>$customerid))."' style='text-decoration:none'>".$val['customer']."</a>";
			//$val['server']="<a href='".U('Server/index')."' style='text-decoration:none'>". $val['server']."</a>";
			//			 $val['master']="<a href='".U('Master/index')."' style='text-decoration:none'>". $val['master']."</a>";
			//			 $val['domain']="<a href='".U('Domain/index')."' style='text-decoration:none'>". $val['domain']."</a>";
			//			 $val['database']="<a href='".U('Database/index')."' style='text-decoration:none'>". $val['database']."</a>";
		
			$tt['tablename']='database_type';
			$tt['fieldvalue']=$val['type'];
			$database_type=D('Descriptions')->where($tt)->find();
			$val['type']=$database_type['title'];
		
		
			$tt1['tablename']='database_size';
			$tt1['fieldvalue']=$val['size'];
			$database_size=D('Descriptions')->where($tt1)->find();
			$val['size']=$database_size['title'];
			$val['e_time']?$val['e_time']=date('Y-m-d',$val['e_time']):'';
		}


        $this->assign('HTTP_HOST', C('DING_HTTP_HOST'));
        $this->assign('page', $page->show());
        $this->assign('data_list', $data_list);
        $this->assign('user_info', $user_info);
        $this->assign('meta_title', '数据库列表');
        $this->display();
	}
	
	
	public function add(){
		
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		if(IS_POST){
			$ding_id=$user_info['userid'];
			$uid=M('Employee')->where(array('ding_id'=>$ding_id))->getField('uid');
			$_POST['s_time']=strtotime($_POST['s_time']);
			$_POST['e_time']=strtotime($_POST['e_time']);
			$_POST['c_user']=$uid;
			$_POST['c_time']=time();
			
			$web=D('Database');
			
			$data=$web->create();
			
			if($data){
			
				$id=$web->add();
			
				if($id){
			
					$this->success('新增成功',U('index'));
			
				}else{
			
					$this->error('新增失败');
			
				}
			
			}else{
			
				$this->error($web->getError());
			
			}

			}else{
				$type=D('Descriptions')->where('tablename="database_type"')->field('fieldvalue as id,title')->select();
				$size=D('Descriptions')->where('tablename="database_size"')->field('fieldvalue as id,title')->select();
				$customer=M("Customer")->field("id,customer_name as title")->select();
				$server=M("Server")->field("id,server_name as title")->select();
				$this->assign('customer', $customer);
				$this->assign('server', $server);
				$this->assign('type', $type);
				$this->assign('size', $size);
				$this->assign('user_info', $user_info);
				$this->assign('meta_title', '新增数据库');
				$this->display();

			}
	}
	
	public function  edit($id){
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		$info=M('Database')->find($id);

			if(IS_POST){
				$server=M('Database');
				
				$_POST['s_time']=strtotime($_POST['s_time']);
				
				$_POST['e_time']=strtotime($_POST['e_time']);
				$data=$server->create();
				
				if($data){
				
					$res=$server->save();
				
					if($res){
				
						$this->success('更新成功',U('index'));
				
					}else{
				
						$this->error('更新失败');
				
					}
				
				}else{
				
					$this->error($server->getError());
				
				}

			}else{
				$type=D('Descriptions')->where('tablename="database_type"')->field('fieldvalue as id,title')->select();
				$size=D('Descriptions')->where('tablename="database_size"')->field('fieldvalue as id,title')->select();
				$customer=M("Customer")->field("id,customer_name as title")->select();
				$server=M("Server")->field("id,server_name as title")->select();
				$this->assign('customer', $customer);
				$this->assign('server', $server);
				$this->assign('type', $type);
				$this->assign('size', $size);
			
				$this->assign('info', $info);
				$this->assign('user_info', $user_info);
				$this->assign('meta_title', '编辑数据库');
				$this->display();
			
			}

	}
	
	/**
	 * 详情页
	 */
	public function databaseinfo(){
		
		$id=I('id');
		$val = M('Database')->where('id='.$id)->find();
		
		$customerid=$val['customer'];
		
		$val['customer']=get_customer_info($val['customer'],"customer_name");
		$val['c_user']=get_user_info($val['c_user'],"username");
		$val['server']=get_fields("Server",$val['server'],"server_name");
		
		$tt['tablename']='database_type';
		$tt['fieldvalue']=$val['type'];
		$database_type=D('Descriptions')->where($tt)->find();
		$val['type']=$database_type['title'];
		
		
		$tt1['tablename']='database_size';
		$tt1['fieldvalue']=$val['size'];
		$database_size=D('Descriptions')->where($tt1)->find();
		$val['size']=$database_size['title'];
				
		$val['s_time']?$val['s_time']=date('Y-m-d',$val['s_time']):'';
		$val['e_time']?$val['e_time']=date('Y-m-d',$val['e_time']):'';
		$val['c_time']?$val['c_time']=date('Y-m-d',$val['c_time']):'';
		
		$this->assign('val', $val);
		$this->assign('meta_title', '数据库详情');
		$this->display();
		
	}
	/**
	 * 删除数据库
	 */
// 	public function delDatabaseAjax(){
	
// 		$id = I('id');
// 		$re=M('Database')->where('id='.$id)->delete();
// 		if ($re) {
// 			$data['info']=1;
// 		}else{
// 			$data['info']=0;
// 		}
// 		$this->ajaxReturn($data);
// 	}
	


}


?>